package com.cn.ant.modules.wechat.service.function.impl;

import com.cn.ant.common.utils.SpringContextHolder;
import com.cn.ant.common.utils.StringUtils;
import com.cn.ant.modules.wechat.entity.TkMovieCustom;
import com.cn.ant.modules.wechat.entity.message.resp.TextMessageResp;
import com.cn.ant.modules.wechat.service.TkMovieCustomService;
import com.cn.ant.modules.wechat.service.WeixinAccountServiceImpl;
import com.cn.ant.modules.wechat.service.function.KeyServiceI;
import com.cn.ant.modules.wechat.utils.MessageUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by huanggenhua on 2015/5/23.
 */
public class CustomInfoService implements KeyServiceI {
    @Override
    public String getKey() {
        return "CX#";
    }

    @Override
    public String excute(String content, TextMessageResp defaultMessage, HttpServletRequest request) {
        // 获取链接的唯一标识
        String cardNo = content.trim().substring(content.indexOf("#")+1);
        String respMessage = "";
        WeixinAccountServiceImpl weixinAccountService = (WeixinAccountServiceImpl) SpringContextHolder.getApplicationContext().getBean("weixinAccountService");
        String accountid = weixinAccountService.findByToUsername(defaultMessage.getFromUserName()).getId();
        if (StringUtils.isNotBlank(cardNo)) {
            TkMovieCustomService customService = SpringContextHolder.getApplicationContext().getBean(TkMovieCustomService.class);
            List<TkMovieCustom> customList = customService.queryByCardNo(cardNo);
            if (customList != null && customList.size() > 0) {
                StringBuffer sb = new StringBuffer("姓名-电话号码-区域-观影时间").append("\n");
                for (int i = 0; i < customList.size(); i++) {
                    TkMovieCustom custom = customList.get(i);
                    String areaName = "";
                    if ("futian".equals(custom.getMovieArea())) {
                        areaName = "福田";
                    } else if ("longgang".equals(custom.getMovieArea())) {
                        areaName = "龙岗";
                    } else if ("longhua".equals(custom.getMovieArea())) {
                        areaName = "龙华";
                    } else if ("baoan".equals(custom.getMovieArea())) {
                        areaName = "宝安";
                    } else if ("shajing".equals(custom.getMovieArea())) {
                        areaName = "沙井";
                    } else if ("yantian".equals(custom.getMovieArea())) {
                        areaName = "盐田";
                    } else {
                        areaName = "深圳";
                    }
                    sb.append((i + 1) + "." + custom.getRealName() + "-" + custom.getMobile() + "-" + areaName + "-" + custom.getMovieTime() + ";").append("\n");
                }

                TextMessageResp textMessage = new TextMessageResp();
                textMessage.setToUserName(defaultMessage.getToUserName());
                textMessage.setFromUserName(defaultMessage.getFromUserName());
                textMessage.setCreateTime(new Date().getTime());
                textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
                textMessage.setContent(sb.toString());
                // 将文本消息对象转换成xml字符串
                respMessage = MessageUtil.textMessageToXml(textMessage);
            } else {
                TextMessageResp textMessage = new TextMessageResp();
                textMessage.setToUserName(defaultMessage.getToUserName());
                textMessage.setFromUserName(defaultMessage.getFromUserName());
                textMessage.setCreateTime(new Date().getTime());
                textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
                textMessage.setContent("暂时没有数据");
                // 将文本消息对象转换成xml字符串
                respMessage = MessageUtil.textMessageToXml(textMessage);
            }
        }
        return respMessage;
    }
}
